package com.smart4s.app.core.positionservice.utils;

import android.util.Base64;
import android.util.DebugUtils;

import org.json.JSONArray;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class DESedeUtil {

    private static String algorithm = "DESede";
    private static String S_CIPHER_ALGORITHM = "DESede/ECB/PKCS5Padding";
    private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
//    private static String DES_KEY = "VThRxkVv7@qkHA1^gLfLg9qA";
//    private static String str = "JMPj/YHMDvsJh2KlHuhjM3p3Qa/GvbFfrwbe+RmNdXqlVs3SeSFniiYo91reCpqt5UUcoUtK62OjwNFLEvm9eXKpf/sk0dOtqtnNpiPl2Y9Kc4WNvSh3JQ==";


    //3DES每次读取8个byte明文数据进行算法加密，然后再输入8个byte的密文，所以我们必须保证加密数据的长度是8byte的整数倍
    public static byte[] padding(String argText) {
        byte[] encrypt = argText.getBytes();
//		LogUtil.e("encrypt","encrypt = " + printHexBinary(encrypt));
//		LogUtil.e("encrypt.length","encrypt.length = " + encrypt.length);
        if (encrypt.length % 8 != 0) { //not a multiple of 8
            //create a new array with a size which is a multiple of 8
            byte[] padded = new byte[encrypt.length + 8 - (encrypt.length % 8)];
//			LogUtil.e("padded","padded = " + printHexBinary(padded));
//			LogUtil.e("padded.length","padded.length = " + padded.length);
            //copy the old array into it
            System.arraycopy(encrypt, 0, padded, 0, encrypt.length);
//			LogUtil.e("padded0","padded0 = " + printHexBinary(padded));
//			LogUtil.e("padded.length0","padded.length0 = " + padded.length);

            encrypt = padded;
//			LogUtil.e("encrypt0","encrypt0 = " + printHexBinary(encrypt));
//			LogUtil.e("encrypt.length0","encrypt.length0 = " + encrypt.length);
        }
        return encrypt;
    }

   /* byte[] data = DESedeUtil.enc(str.getBytes(),DESedeUtil.padding(DES_KEY));
    String content = Base64.encodeToString(data,Base64.DEFAULT);
        System.out.print(content);*/

    //加密
    public static byte[] enc(byte[] byteaPlainText, byte[] keybyte) {
        try {
            SecretKey skSecretkey = new SecretKeySpec(keybyte, algorithm);
            Cipher cipher = Cipher.getInstance(S_CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, skSecretkey);
            byte[] byteaCryptograph = cipher.doFinal(byteaPlainText);
            return byteaCryptograph;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    //解密
    public static byte[] dec(byte[] byteaCryptograph, byte[] keybyte) {
        try {
            SecretKey skSecretkey = new SecretKeySpec(keybyte, algorithm);
            Cipher cCipher = Cipher.getInstance(S_CIPHER_ALGORITHM);
            cCipher.init(Cipher.DECRYPT_MODE, skSecretkey);
            byte[] byteaPlainText = cCipher.doFinal(byteaCryptograph);
            return byteaPlainText;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    private static String printHexBinary(byte[] data) {
        StringBuilder r = new StringBuilder(data.length * 2);
        for (byte b : data) {
            r.append(hexCode[(b >> 4) & 0xF]);
            r.append(hexCode[(b & 0xF)]);
        }
        return r.toString();
    }

}
